from flask import Blueprint,jsonify, request
from main.BSL.friendnoticeBSL import *
from main.BSL import userBSL, noticeBSL
from main.utils import InterfaceHelper,RequestHelper

friendnotice = Blueprint('friendnotice',__name__)


@friendnotice.route('/friendnotice/add', methods=["POST"])
def add_friendnotice():
    res = {}
    dic = RequestHelper.formToDict(request)
    user = userBSL.query_username(dic['username'])
    dic['user_id'] = user.id
    friendnotice = JSONHelper.dictToObj(dic, FriendNotice)
    try:
        insert(friendnotice)
        res['status'] = 1
    except:
        res['status'] = 0
    return jsonify(res)

@friendnotice.route('/friendnotice/<int:user_id>', methods = ['GET'])
def query_friendnotice(user_id):
    res ={}
    friendnotice = query_user_id_is_notice(user_id, False)
    if friendnotice:
        # res['friendnotice'] = friendnotice.to_json()
        friendnotice.is_notice = True
        update(friendnotice)
        res['status'] = 1
    else:
        res['status'] = 0
    return jsonify(res)


@friendnotice.route('/friendnotice/update', methods=['POST'])
def friendnotice_update(user_id):
    res = {}
    friendnotice = query_user_id_is_notice(user_id, False)
    if friendnotice:
        friendnotice.is_notice = True
        update(friendnotice)
        res['status'] = 1
    else:
        res['status'] = 0
    return jsonify(res)


